package com.xuecheng.media.controller;

import com.aliyuncs.exceptions.ClientException;
import com.xuecheng.commons.enums.ErrorCode;
import com.xuecheng.commons.model.dto.*;
import com.xuecheng.commons.model.vo.*;
import com.xuecheng.commons.utils.BeanHelper;
import com.xuecheng.media.domain.Media;
import com.xuecheng.web.exceptions.BusinessException;
import org.apache.commons.lang3.ObjectUtils;
import org.springframework.web.bind.annotation.*;
import com.xuecheng.media.service.MediaService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;

import javax.xml.ws.Service;

/**
 * <p>
 * 媒资信息前端控制器
 * </p>
 *
 * @author 七安
 */
@Slf4j
@RestController
@RequestMapping("/media")
public class MediaController {

    @Autowired
    private MediaService mediaService;

    /**
     * 获取阿里云上传token
     *
     * @return 上传地址，上传凭证
     */
    @PostMapping("/vod-token")
    public ResponseResult<VodUploadVo> getUploadToken(@RequestBody VodUploadDto vodUploadDto) throws BusinessException, ClientException {
        return mediaService.getUploadToken(vodUploadDto);
    }

    /**
     * 刷新上传token
     *
     * @param videoId 视频id
     * @return 上传地址和凭证
     */
    @GetMapping("/refresh-vod-token/{videoId}")
    public ResponseResult<VodUploadVo> refreshUploadToken(@PathVariable("videoId") String videoId) throws ClientException {
        return mediaService.refreshUploadToken(videoId);
    }

    /**
     * 保存媒资信息
     *
     * @param mediaDto 要保存的内容
     * @return 结果
     */
    @PostMapping
    public ResponseResult saveMedia(@RequestBody MediaDto mediaDto) throws ClientException, BusinessException {
        //调用service
        return mediaService.saveMedia(mediaDto);
    }

    /**
     * 分页查询媒资信息
     *
     * @param pageRequest   分页条数...
     * @param mediaQueryDto 媒资查询封装类
     * @return 查到的数据
     */
    @PostMapping("/list")
    public PageResponseResult<MediaVo> findMediaList(PageRequest pageRequest, @RequestBody MediaQueryDto mediaQueryDto) {
        return mediaService.findMediaList(pageRequest, mediaQueryDto);
    }

    /**
     * 审核媒资
     *
     * @param mediaAuditDto 审核意见和状态
     * @return 审核结果
     */
    @PutMapping("/audit")
    public ResponseResult verifyMedia(@RequestBody MediaAuditDto mediaAuditDto) throws BusinessException {
        return mediaService.verifyMedia(mediaAuditDto);
    }

    /**
     * 根据id查询媒资信息
     *
     * @param mediaId 媒资id
     * @return 查到的结果
     */
    @GetMapping("/{mediaId}")
    public ResponseResult<MediaVo> findMediaById(@PathVariable Long mediaId) throws BusinessException {
        Media media = mediaService.getById(mediaId);
        if (ObjectUtils.isEmpty(media)) {
            throw new BusinessException(ErrorCode.DATAERROR);
        }
        //转成对应的Vo对象
        MediaVo mediaVo = BeanHelper.copyProperties(media, MediaVo.class);
        return ResponseResult.okResult(mediaVo);
    }

    /**
     * 根据id预览视频
     *
     * @param mediaId 媒资id
     * @return 结果
     */
    @GetMapping("/preview/{mediaId}")
    public ResponseResult<String> getMediaUrl(@PathVariable Long mediaId) throws BusinessException {
        return mediaService.getMediaUrl(mediaId);
    }

    /**
     * 根据id删除媒资
     *
     * @param mediaId 要删除的媒资id
     * @return 结果
     */
    @DeleteMapping("/{mediaId}")
    public ResponseResult<Boolean> deleteMediaById(@PathVariable Long mediaId) throws Exception {
        return mediaService.deleteMediaById(mediaId);
    }

    /**
     * 根据id获取媒资URL地址
     * @param mediaId id
     * @return 结果
     */
    @GetMapping("/media-url/{mediaId}")
    public ResponseResult<ResourceVo> gainMediaUrl(@PathVariable Long mediaId) throws BusinessException, ClientException {
        return mediaService.gainMediaUrl(mediaId);
    }
}
